home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 6 / QRZ Ham Radio Callsign Database - Volume 6.iso / pc / files / mac / sftkisrc.hqx / SoftKiss.src.1.8 / dbo / dbo_printf.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-19  |  6.7 KB  |  305 lines

  1. /*
  2.  * dbo_stdout.c - debugging print system
  3.  * by Aaron Wohl
  4.  * Public domain
  5.  * 6393 Penn Ave #303
  6.  * Pittsburgh PA, 15208
  7.  * home: 412-731-6159
  8.  * work: 412-268-5032
  9.  */
  10.  
  11. #ifdef DBO_ENABLED
  12. #include "dbo_stdio.h"
  13. #include "dbo_font.h"
  14. #include <string.h>
  15.  
  16. /*
  17.  * per field options
  18.  */
  19. #define dbo_OPT_LONG     (1)
  20. #define dbo_OPT_MODF     (2)
  21. #define dbo_OPT_LEFT     (4)
  22. #define dbo_OPT_BASE16  (8)
  23. #define dbo_OPT_MINUS    (16)
  24.  
  25. /*
  26.  * use this file for stdout
  27.  */
  28. static dbo_FILE dbo_stdout_FILE;
  29.  
  30. /*
  31.  * initialize the passed dbo file for full screen
  32.  * on startup screen
  33.  */
  34. void dbo_fopen(dbo_FILE_pt af)
  35. {
  36.     static Point zp={0,0};
  37.     af->win_tl=zp;
  38.     af->win_cur=zp;
  39.     af->win_br.v= (*((short *)0x0C22)); /*ColLines*/
  40.     af->win_br.h= (*((short *)0x0C20)); /*RowBits*/
  41.     af->mem= (*((unsigned char **)0x824)); /*ScrnBase*/
  42.     af->row_bytes= (*((short *)0x106));    /*ScreenRow*/
  43.     af->wrap=TRUE;
  44.     af->nl_extra=4;                /*white space at end of line*/
  45.     af->plimit=400;                /*max bytes out per printf*/
  46.     af->int_is4=FALSE;            /*default to 2 byte integers*/
  47.     af->just_kidding=FALSE;
  48.     af->dbo_guard1=dbo_GUARD1;
  49.     af->dbo_guard2=dbo_GUARD2;
  50. }
  51.  
  52. /*
  53.  * write one character to a file
  54.  */
  55. void dbo_fputc(register dbo_FILE_pt af,char ich)
  56. {
  57.     int i;
  58.     unsigned char ch=ich&0x7F;
  59.     unsigned char *mem_base;
  60.     unsigned char *font_info= &dbo_draw_font[ch][0];
  61.     if(af->just_kidding)
  62.         return;
  63.     if(ch=='\n') {
  64.         long line_len=af->win_br.h-af->win_cur.h;
  65.         mem_base=af->mem+(af->win_cur.h>>3)+af->win_cur.v*af->row_bytes;
  66.         if(line_len>af->nl_extra)
  67.             line_len=af->nl_extra;
  68.         if(line_len>0)
  69.           for(i=0;i<DBO_LINE_HEIGHT;i++,mem_base+=af->row_bytes)
  70.             memset(mem_base,0,line_len);
  71.         af->win_cur.v+=DBO_LINE_HEIGHT;
  72.         af->win_cur.h=af->win_tl.h;
  73.         return;
  74.     }
  75.     if((af->win_cur.h+DBO_CHAR_PIXEL_WIDTH)>=af->win_br.h) {
  76.         if(!af->wrap)
  77.             return;
  78.         af->win_cur.h=af->win_tl.h;
  79.         af->win_cur.v+=DBO_LINE_HEIGHT;
  80.     }
  81.     if((af->win_cur.v+(DBO_LINE_HEIGHT-1))>=af->win_br.v)
  82.         af->win_cur.v=af->win_tl.v;
  83.     mem_base=af->mem+(af->win_cur.h>>3)+af->win_cur.v*af->row_bytes;
  84.      for(i=0;i<DBO_LINE_HEIGHT;i++,mem_base+=af->row_bytes)
  85.         *mem_base = *font_info++;
  86.     af->win_cur.h+=8;
  87. }
  88.  
  89. /*
  90.  * v array version of printf
  91.  */
  92. int dbo_vfprintf(dbo_FILE_pt af,const char *fmt,va_list ap)
  93. {
  94.     register int ch;
  95.     long plimit;        /*max chars left to print*/
  96.     int def_options;
  97.     if((af==dbo_stdout)||            /*if want stdout*/
  98.         ((((long)af)&0x1)!=0)||        /*or invalid address for file structure*/
  99.         (af->dbo_guard1!=dbo_GUARD1)|| /*or not a vailid file structure*/
  100.         (af->dbo_guard2!=dbo_GUARD2)) {
  101.         af= &dbo_stdout_FILE;
  102.         if((af->dbo_guard1!=dbo_GUARD1)||(af->dbo_guard2!=dbo_GUARD2))
  103.             dbo_fopen(af);
  104.     }
  105.     plimit=af->plimit;
  106.     def_options=(af->int_is4?dbo_OPT_LONG:0);
  107.     if(af->just_kidding)goto done;
  108.     for (ch=*fmt;ch!=0;ch=*++fmt) {
  109.         long field_width=0;
  110.         int options=def_options;
  111.         char num_buf[40];            /*convert numbers to text here*/
  112. #define NUM_BUF_END (&num_buf[(sizeof(num_buf)-1)])
  113.         char *sout= NUM_BUF_END;    /*string to print*/
  114.         int slen= -1;                /*limit of chars to print -1 for till null*/
  115.         unsigned long num=0;
  116.         char fill_ch=' ';
  117.         *NUM_BUF_END=0;
  118.         if(plimit<=0)
  119.              goto done;    /*printed as much as allowed*/
  120.         if(ch==0)
  121.             return;            /*or as desired*/
  122.         if(ch!='%') {
  123.             put_ch:                    /*put current character unquoted*/
  124.             if(ch==0)goto done;
  125.             dbo_fputc(af,ch);
  126.             plimit--;
  127.             continue;
  128.         }
  129.         if((ch= *++fmt)==0)
  130.             goto done;
  131.         /*parse modifyers and options*/
  132.         for(;;ch= *++fmt) {
  133.             if(ch=='0')
  134.                 fill_ch='0';
  135.             else if(ch=='#')
  136.                 options|=dbo_OPT_MODF;
  137.             else if((ch=='l')||(ch=='L'))
  138.                 options|=dbo_OPT_LONG;
  139.             else if((ch=='h')||(ch=='H'))
  140.                 options&= ~dbo_OPT_LONG;
  141.             else if(ch=='-')
  142.                 options|=dbo_OPT_LEFT;
  143.             else if((ch>='0')&&(ch<='9')) {
  144.                     for(;ch>='0' && ch<='9';ch=*++fmt)
  145.                         field_width= field_width*10+(ch-'0');
  146.                     fmt--;
  147.             } else break;
  148.         }
  149.         switch(ch) {
  150.             case 'x':
  151.             case 'X':
  152.                 options|=dbo_OPT_BASE16;
  153.                 /*fall thru*/
  154.             case 'u':
  155.                 if(options&dbo_OPT_LONG)
  156.                     num=va_arg(ap,unsigned long);
  157.                 else
  158.                     num=va_arg(ap,unsigned short);
  159.                 goto out_num;
  160.             case 'd':
  161.                 if(options&dbo_OPT_LONG)
  162.                     num=va_arg(ap,long);
  163.                 else
  164.                     num=va_arg(ap,short);
  165.                 if(((long)num)<0) {
  166.                     options|=dbo_OPT_MINUS;
  167.                     num= -(((long)num));
  168.                 }
  169.               out_num:
  170.                 if(options&dbo_OPT_BASE16)
  171.                     do{
  172.                       *--sout= "0123456789abcdef"[num&0xf];
  173.                       num>>=4;
  174.                     }while(num!=0);
  175.                 else
  176.                     do{
  177.                       *--sout=(num % 10)+'0';
  178.                       num/=10;
  179.                     }while(num!=0);
  180.                 if(options&dbo_OPT_MINUS)
  181.                     *--sout='-';
  182.                 goto put_string;
  183.             case 's':
  184.                 sout=va_arg(ap,char *);
  185.                 /*if have a bogus pointer say so*/
  186.                 if(sout==0)sout= (char*)"\p(null)";
  187.                 if(!(options&dbo_OPT_MODF))
  188.                     goto put_string;
  189.                 slen= (*sout)&0xff;
  190.                 sout++;
  191.                 goto put_string;
  192.             case 'c':
  193.                 if(options&dbo_OPT_LONG)
  194.                     num=va_arg(ap,long);
  195.                 else
  196.                     num=va_arg(ap,short);
  197.                 *--sout=num;
  198.                 slen=1;
  199.                 goto put_string;
  200.                 break;
  201.             case '%':
  202.             default:
  203.                 goto put_ch;
  204.         }
  205.     put_string:
  206.     /*
  207.      * sout - string to write
  208.      * slen - <0 for null terminated or length to write
  209.      * fieldwidth > 0 - justify in this field width
  210.      * fieldwidth = 0 - no justify
  211.      * options - see options defs above
  212.      */
  213.      /*if right justifying put out leading fill*/
  214.      if(slen<0)slen=strlen(sout);
  215.      if(field_width>0) {
  216.          if(slen>field_width)slen=field_width;
  217.          field_width-=slen;
  218.          if((options&dbo_OPT_LEFT)==0)
  219.              while(field_width>0) {
  220.                  dbo_fputc(af,fill_ch);
  221.                 if(--plimit <=0) goto done;
  222.                 field_width--;
  223.             }
  224.     }
  225.     /*actually put out a field*/
  226.      while(--slen >=0) {
  227.          dbo_fputc(af,*sout++);
  228.         if(--plimit <=0)  goto done;
  229.     }
  230.     /*if left justifying, fill right side of field*/
  231.      while(field_width>0) {
  232.          dbo_fputc(af,fill_ch);
  233.         if(--plimit <=0)  goto done;
  234.         field_width--;
  235.     }
  236.     }
  237. done:
  238.   return af->plimit-plimit;
  239. }
  240.  
  241. /*
  242.  * debugging fprintf
  243.  */
  244. int dbo_fprintf(dbo_FILE *af,const char *fmt,...)
  245. {
  246.     return(dbo_vfprintf(af,fmt,__va(fmt)));
  247. }
  248.  
  249. /*
  250.  * debugging vprintf
  251.  */
  252. int dbo_vprintf(const char *fmt,void *p)
  253. {
  254.     return(dbo_vfprintf(dbo_stdout,fmt,p));
  255. }
  256.  
  257. /*
  258.  * debugging printf
  259.  */
  260. int dbo_printf(const char *fmt,...)
  261. {
  262.     return(dbo_vprintf(fmt,__va(fmt)));
  263. }
  264.  
  265. /*
  266.  * position cursor in the stdout dbo window
  267.  */
  268. void dbo_goto(int x,int y)
  269. {
  270.     dbo_fgoto(dbo_stdout,x,y);
  271. }
  272.  
  273. /*
  274.  * position cursor in a dbo window
  275.  */
  276. void dbo_fgoto(dbo_FILE *af,int x,int y)
  277. {
  278.     af->win_cur.h=af->win_tl.h+DBO_CHAR_PIXEL_WIDTH*x;
  279.     af->win_cur.v=af->win_tl.v+DBO_LINE_HEIGHT*y;
  280. }
  281.  
  282. /*
  283.  * clear window
  284.  */
  285. void dbo_clear(void)
  286. {
  287.     dbo_fclear(dbo_stdout);
  288. }
  289.  
  290. /*
  291.  * clear file window
  292.  */
  293. void dbo_fclear(dbo_FILE *af)
  294. {
  295.     long i;
  296.     unsigned char *mem_base=af->mem+(af->win_tl.h>>3)+((long)af->win_tl.v)*af->row_bytes;
  297.     long line_len=(af->win_br.h-af->win_tl.h)>>3;
  298.     long win_height=(af->win_br.v-af->win_tl.v);
  299.     if(af->just_kidding)
  300.         return;
  301.     for(i=0;i<win_height;i++,mem_base+=af->row_bytes)
  302.         memset(mem_base,0,line_len);
  303. }
  304. #endif
  305.